﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyLocalChurch.Constants;
using MyLocalChurch.Models;
using RKConsulting.Helpers;
using RKConsulting.Web.Helpers;

namespace MyLocalChurch.ChurchServices
{
    public partial class EditChurchService : System.Web.UI.Page
    {
        private readonly MyContext _context = MyContextSingleton.Instance;
        private ChurchService _churchService;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack)
                return;

            ddlDayOfWeek.BindEnumValues<DayOfWeek>();

            _churchService = GetChurchService();
            txtStartTime.Text = _churchService.StartTime;
            chkActive.Checked = _churchService.IsActive;
        }

        protected ChurchService GetChurchService()
        {
            var id = QueryHelper.GetIntQueryParam(QueryParameters.Id);
            return _context.ChurchServices.FirstOrDefault(cs => cs.Id == id);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Page.Validate();
            if (!Page.IsValid)
                return;

            _churchService = GetChurchService();
            _churchService.DayOfWeek = EnumHelper.Parse<DayOfWeek>(ddlDayOfWeek.SelectedValue);
            _churchService.StartTime = txtStartTime.Text;
            _churchService.Description = txtDescription.Text;
            _churchService.IsActive = chkActive.Checked;

            _context.Entry(_churchService).State = EntityState.Modified;
            _context.SaveChanges();

            Response.Redirect("Default.aspx");
        }
    }
}